package com.qf.controller;

import com.qf.model.Address;
import com.qf.model.Car;
import com.qf.model.Order;
import com.qf.model.User;
import com.qf.service.AddressService;
import com.qf.service.CarService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CarServiceImpl;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseServlet {
    CarService carService=new CarServiceImpl();
    AddressService addressService=new AddressServiceImpl();
    OrderService service=new OrderServiceImpl();
    public String skipOrder(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        Object user = session.getAttribute("loginUser");
        if(user==null){
            return "REDIRECT:login.jsp";
        }
        List<Car> list=null;
        int uid=((User)user).getId();
        list=carService.getAllCar(uid);
        request.setAttribute("carList",list);

        //还要根据uid拿到地址集合
        List<Address> addressList=addressService.getAllAddress(uid);
        request.setAttribute("addressList",addressList);




        return "FORWARD:order.jsp";
    }


    /**
     * 生成订单的方法
     * @param request
     * @param response
     * @return
     * http://localhost:8081/order?action=addOrder&aid=2&sum=8398.00
     */
    public String addOrder(HttpServletRequest request,HttpServletResponse response){
        String aidStr=request.getParameter("aid");
        String sumStr=request.getParameter("sum");
        HttpSession session = request.getSession();
        Object user = session.getAttribute("loginUser");
        if(user==null){
            return "REDIRECT:login.jsp";
        }
        int uid=((User)user).getId();
        int a=service.addOrder(aidStr,sumStr,uid);
        return "REDIRECT:order?action=getOrders";
    }


    public String getOrders(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        Object user = session.getAttribute("loginUser");
        if(user==null){
            return "REDIRECT:login.jsp";
        }
        int uid=((User)user).getId();
        List<Order> list=service.getOrders(uid);
        request.setAttribute("orderList",list);
        for(Order o:list){
            System.out.println(o);
        }



        return "FORWARD:orderList.jsp";
    }


    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){
        String oidStr=request.getParameter("oid");
        Order order=service.getOrderDetails(oidStr);
        System.out.println(order.toString());
        request.setAttribute("order",order);
        return "FORWARD:orderDetail.jsp";
    }

}
